package com.ruoyi.analysis.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.analysis.domain.AnalyzePayloadReq;
import com.ruoyi.analysis.domain.AnalyzePayloadResp;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.analysis.domain.OfflineHistory;
import com.ruoyi.analysis.service.IOfflineHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 离线分析历史Controller
 * 
 * @author weihanqiang
 * @date 2025-06-19
 */
@RestController
@RequestMapping("/analysis/offline")
public class OfflineHistoryController extends BaseController
{
    @Autowired
    private IOfflineHistoryService offlineHistoryService;

    /**
     * 分析载荷数据
     */
    @PreAuthorize("@ss.hasPermi('analysis:offline:getAnalyzePayload')")
    @PostMapping("/getAnalyzePayload")
    public AjaxResult analyzePayload(@RequestBody AnalyzePayloadReq req)
    {
        try {
            AnalyzePayloadResp resp = offlineHistoryService.analyzePayload(req);
            return AjaxResult.success("分析成功", resp);
        } catch (Exception e) {
            return AjaxResult.error("分析失败: " + e.getMessage());
        }
    }

    /**
     * 查询离线分析历史列表
     */
    @PreAuthorize("@ss.hasPermi('analysis:offline:list')")
    @GetMapping("/list")
    public TableDataInfo list(OfflineHistory offlineHistory)
    {
        startPage();
        List<OfflineHistory> list = offlineHistoryService.selectOfflineHistoryList(offlineHistory);
        return getDataTable(list);
    }

    /**
     * 导出离线分析历史列表
     */
    @PreAuthorize("@ss.hasPermi('analysis:offline:export')")
    @Log(title = "离线分析历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OfflineHistory offlineHistory)
    {
        List<OfflineHistory> list = offlineHistoryService.selectOfflineHistoryList(offlineHistory);
        ExcelUtil<OfflineHistory> util = new ExcelUtil<OfflineHistory>(OfflineHistory.class);
        util.exportExcel(response, list, "离线分析历史数据");
    }

    /**
     * 获取离线分析历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('analysis:offline:query')")
    @GetMapping(value = "/{offlineResultId}")
    public AjaxResult getInfo(@PathVariable("offlineResultId") Long offlineResultId)
    {
        return success(offlineHistoryService.selectOfflineHistoryByOfflineResultId(offlineResultId));
    }

    /**
     * 新增离线分析历史
     */
    @PreAuthorize("@ss.hasPermi('analysis:offline:add')")
    @Log(title = "离线分析历史", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OfflineHistory offlineHistory)
    {
        offlineHistory.setCreateTime(DateUtils.getNowDate());
        offlineHistory.setCreator(getUsername());
        offlineHistory.setCreatorId(getUserId());
        return toAjax(offlineHistoryService.insertOfflineHistory(offlineHistory));
    }

    /**
     * 修改离线分析历史
     */
    @PreAuthorize("@ss.hasPermi('analysis:offline:edit')")
    @Log(title = "离线分析历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OfflineHistory offlineHistory)
    {
        return toAjax(offlineHistoryService.updateOfflineHistory(offlineHistory));
    }

    /**
     * 删除离线分析历史
     */
    @PreAuthorize("@ss.hasPermi('analysis:offline:remove')")
    @Log(title = "离线分析历史", businessType = BusinessType.DELETE)
	@DeleteMapping("/{offlineResultIds}")
    public AjaxResult remove(@PathVariable Long[] offlineResultIds)
    {
        return toAjax(offlineHistoryService.deleteOfflineHistoryByOfflineResultIds(offlineResultIds));
    }
}